package com.efast.dmom.equipment.service.masterdata.impl;

import com.alibaba.fastjson.JSONObject;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.hotpointrecord.*;
import com.efast.dmom.equipment.dao.masterdata.EquipHotPointRecordDao;
import com.efast.dmom.equipment.entity.EquipHotPointRecord;
import com.efast.dmom.equipment.service.masterdata.IEquipHotPointRecordRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class EquipHotPointRecordServiceImpl implements IEquipHotPointRecordRService {

	@Autowired
	private EquipHotPointRecordDao equipHotPointRecordDao;


	@Override
	public void saveEquipHotPointRecord(EquipHotPointRecord bean) {

		bean.setCompanyCode(SpringWebUtils.getRequestCompany());
		bean.setCreateTime(new Date());
		bean.setCreateUser(SpringWebUtils.getRequestUser());
		bean.setLastUpdateTime(new Date());
		bean.setLastUpdateUser(SpringWebUtils.getRequestUser());
		//如果可以查询到数据，则编辑
		if(equipHotPointRecordDao.queryEquipHotPointRecord(bean)!=null){
			//编辑热点图
		     equipHotPointRecordDao.editEquipHotPointRecord(bean);
		}else{
			//保存热点图
			equipHotPointRecordDao.saveEquipHotPointRecord(bean);
		}
	}

	@Override
	public EquipHotPointRecord updateEquipHotPointRecord(EquipHotPointRecord bean) {
		String desc=equipHotPointRecordDao.queryEquipHotPointRecord(bean).getHotDesc();
        //字符串转json
		JSONObject jo = JSONObject.parseObject(desc);
        //json转对象
		HotParentBean hotParentBean = (HotParentBean) JSONObject.toJavaObject(jo, HotParentBean.class);
		List<Integer> wkList=new ArrayList<>();
		List<Integer> areaList=new ArrayList<>();
		List<Integer> plList=new ArrayList<>();
		List<Integer> qeuipList=new ArrayList<>();
		if(hotParentBean.getHotAreas()!=null&&hotParentBean.getHotAreas().size()>0){
			for(HotAreasBean h:hotParentBean.getHotAreas()){
                  if("workshop".equals(h.getNode().get("type"))){
					  wkList.add(Integer.parseInt(h.getNode().get("idT")));
				  }else if("area".equals(h.getNode().get("type"))){
					  areaList.add(Integer.parseInt(h.getNode().get("idT")));
				  }else if("line".equals(h.getNode().get("type"))){
					  plList.add(Integer.parseInt(h.getNode().get("idT")));
				  }else if("equip".equals(h.getNode().get("type"))){
					  qeuipList.add(Integer.parseInt(h.getNode().get("id")));
				}
			}
		}
		Map<Integer,TmBasWorkshopBean> map1=new HashMap<>();
		if(wkList.size()>0) {
			List<TmBasWorkshopBean> tmBasWorkshopList = equipHotPointRecordDao.queryWorkShipListInfo(wkList);
			if(tmBasWorkshopList!=null&&tmBasWorkshopList.size()>0){
				for(TmBasWorkshopBean t:tmBasWorkshopList){
					map1.put(111,t);
//					t.getTmBasWorkshopId()
				}
			}
		}
		Map<Integer,TmBasAreaBean> map2=new HashMap<>();
		if(areaList.size()>0) {
			List<TmBasAreaBean> tmBasAreaList = equipHotPointRecordDao.queryAreaListInfo(areaList);
			if(tmBasAreaList!=null&&tmBasAreaList.size()>0){
				for(TmBasAreaBean t:tmBasAreaList){
					map2.put(22222,t);
//					t.getTmBasAreaId()
				}
			}
		}
		Map<Integer,TmBasProductionLineBean> map3=new HashMap<>();
		if(plList.size()>0) {
			List<TmBasProductionLineBean> tmBasProductionLineList = equipHotPointRecordDao.queryProductionLineListInfo(plList);
			if(tmBasProductionLineList!=null&&tmBasProductionLineList.size()>0){
				for(TmBasProductionLineBean t:tmBasProductionLineList){
					map3.put(33333,t);
//					t.getTmBasProductionLineId()
				}
			}
		}
		Map<Integer,EquipBaseInfoBean> map4 =new HashMap<>();
		if(qeuipList.size()>0) {
		    List<EquipBaseInfoBean> tmBasEquipBaseInfoList=equipHotPointRecordDao.queryEquipBaseListInfo(qeuipList);
			if(tmBasEquipBaseInfoList!=null&&tmBasEquipBaseInfoList.size()>0){
				for(EquipBaseInfoBean t:tmBasEquipBaseInfoList){
					map4.put(t.getId(),t);
				}
			}
		}
        //先更新父级信息(判断是哪种父级)
		List<Integer> idList=new  ArrayList<>();
		idList.add(hotParentBean.getIdT());
		if("plant".equals(hotParentBean.getType())){
			TmBasPlantBean tmBasPlant=equipHotPointRecordDao.queryPlantInfo(hotParentBean.getIdT());
			//tmBasPlant.getPlantNo()
			hotParentBean.setNo("1111");
			//tmBasPlant.getPlantNamec()
			hotParentBean.setName("33333");
			//tmBasPlant.getPlantNamec()
			hotParentBean.setTitle("44444");
			//tmBasPlant.getPlantNamec()
			hotParentBean.setOption_desc("555555");
			hotParentBean.setAttId(tmBasPlant.getAttId());
			if(hotParentBean.getImg().indexOf("=")>0){
			     String img=hotParentBean.getImg().split("=")[0]+"="+tmBasPlant.getAttId();
			     hotParentBean.setImg(img);
			}

		}else if("workshop".equals(hotParentBean.getType())){
			List<TmBasWorkshopBean> tmBasWorkshopList = equipHotPointRecordDao.queryWorkShipListInfo(idList);
			//tmBasWorkshopList.get(0).getWorkshopNo()
			hotParentBean.setNo("22222");
			//tmBasWorkshopList.get(0).getWorkshopNamec()
			hotParentBean.setName("22222");
			//tmBasWorkshopList.get(0).getWorkshopNamec()
			hotParentBean.setTitle("22222");
			//tmBasWorkshopList.get(0).getWorkshopNamec(
			hotParentBean.setOption_desc("22222");
			hotParentBean.setAttId(tmBasWorkshopList.get(0).getAttId());
			if(hotParentBean.getImg().indexOf("=")>0){
				String img=hotParentBean.getImg().split("=")[0]+"="+tmBasWorkshopList.get(0).getAttId();
				hotParentBean.setImg(img);
			}
		}else if("area".equals(hotParentBean.getType())){
			List<TmBasAreaBean> tmBasAreaList = equipHotPointRecordDao.queryAreaListInfo(idList);
			//tmBasAreaList.get(0).getAreaNo()
			hotParentBean.setNo("111111");
			//tmBasAreaList.get(0).getAreaDesc()
			hotParentBean.setName("111111");
			//tmBasAreaList.get(0).getAreaDesc()
			hotParentBean.setTitle("111111");
			//tmBasAreaList.get(0).getAreaDesc()
			hotParentBean.setOption_desc("111111");
			hotParentBean.setAttId(tmBasAreaList.get(0).getAttId());
			if(hotParentBean.getImg().indexOf("=")>0){
				String img=hotParentBean.getImg().split("=")[0]+"="+tmBasAreaList.get(0).getAttId();
				hotParentBean.setImg(img);
			}
		}else if("line".equals(hotParentBean.getType())){
			List<TmBasProductionLineBean> tmBasProductionLineList = equipHotPointRecordDao.queryProductionLineListInfo(idList);
			//tmBasProductionLineList.get(0).getLineName()
			hotParentBean.setName("111111");
			//tmBasProductionLineList.get(0).getLineName()
			hotParentBean.setTitle("111111");
			//tmBasProductionLineList.get(0).getLineName()
			hotParentBean.setOption_desc("111111");
			hotParentBean.setAttId(tmBasProductionLineList.get(0).getAttId());
			if(hotParentBean.getImg().indexOf("=")>0){
				String img=hotParentBean.getImg().split("=")[0]+"="+tmBasProductionLineList.get(0).getAttId();
				hotParentBean.setImg(img);
			}
		}

		if(hotParentBean.getHotAreas()!=null&&hotParentBean.getHotAreas().size()>0){
			for(HotAreasBean h:hotParentBean.getHotAreas()){
				if("workshop".equals(h.getNode().get("type"))){
					TmBasWorkshopBean tmBasWorkshop = map1.get(Integer.parseInt(h.getNode().get("idT")));
					//tmBasWorkshop.getWorkshopNamec()
					h.setName("111111");
					//tmBasWorkshop.getWorkshopNo()
					h.getNode().put("no","111111");
					//tmBasWorkshop.getWorkshopNamec()
					h.getNode().put("name","111111");
					//tmBasWorkshop.getWorkshopNamec()
					h.getNode().put("title","111111");
					//tmBasWorkshop.getWorkshopNamec()
					h.getNode().put("value","111111");
					//tmBasWorkshop.getWorkshopNamec()
					h.getNode().put("option_desc","111111");
					h.getNode().put("attId",tmBasWorkshop.getAttId()+"");
					if(h.getNode().get("img").indexOf("=")>0){
						String img=h.getNode().get("img").split("=")[0]+"="+tmBasWorkshop.getAttId();
						h.getNode().put("img",img);
					}
				}else if("area".equals(h.getNode().get("type"))){
					TmBasAreaBean tmBasArea = map2.get(Integer.parseInt(h.getNode().get("idT")));
					//tmBasArea.getAreaDesc()
					h.setName("111111");
					//tmBasArea.getWorkshopNo()
					h.getNode().put("no","111111");
					//tmBasArea.getAreaNo()
					h.getNode().put("name","111111");
					//tmBasArea.getAreaDesc()
					h.getNode().put("title","111111");
					//tmBasArea.getAreaDesc()
					h.getNode().put("value","111111");
					//tmBasArea.getAreaDesc()
					h.getNode().put("option_desc","111111");
					h.getNode().put("attId",tmBasArea.getAttId()+"");
					if(h.getNode().get("img").indexOf("=")>0){
						String img=h.getNode().get("img").split("=")[0]+"="+tmBasArea.getAttId();
						h.getNode().put("img",img);
					}
				}else if("line".equals(h.getNode().get("type"))){
					TmBasProductionLineBean tmBasProductionLine= map3.get(Integer.parseInt(h.getNode().get("idT")));
					//tmBasProductionLine.getLineName()
					h.setName("111111");
					//tmBasProductionLine.getLineName()
					h.getNode().put("name","111111");
					//tmBasProductionLine.getLineName()
					h.getNode().put("title","111111");
					//tmBasProductionLine.getLineName()
					h.getNode().put("value","111111");
					//tmBasProductionLine.getLineName()
					h.getNode().put("option_desc","111111");
					h.getNode().put("attId",tmBasProductionLine.getAttId()+"");
					if(h.getNode().get("img").indexOf("=")>0){
						String img=h.getNode().get("img").split("=")[0]+"="+tmBasProductionLine.getAttId();
						h.getNode().put("img",img);
					}
				}else if("equip".equals(h.getNode().get("type"))){
					EquipBaseInfoBean equipBaseInfoBean= map4.get(Integer.parseInt(h.getNode().get("id")));
					h.setName(equipBaseInfoBean.getEquipName());
					h.getNode().put("line",equipBaseInfoBean.getLine());
					h.getNode().put("name",equipBaseInfoBean.getEquipName());
					h.getNode().put("size",equipBaseInfoBean.getSize());
					h.getNode().put("level",equipBaseInfoBean.getLevel());
					h.getNode().put("value",equipBaseInfoBean.getEquipName());
					h.getNode().put("remark",equipBaseInfoBean.getRemark());
					h.getNode().put("weight",equipBaseInfoBean.getWeight());
					h.getNode().put("line_id",equipBaseInfoBean.getLineId()+"");
					h.getNode().put("location",equipBaseInfoBean.getLocation());
					h.getNode().put("operator",equipBaseInfoBean.getOperator());
					h.getNode().put("supplier",equipBaseInfoBean.getSupplier());
					h.getNode().put("use_dept",equipBaseInfoBean.getUseDept());
					h.getNode().put("equip_code",equipBaseInfoBean.getEquipCode());
					h.getNode().put("equip_name",equipBaseInfoBean.getEquipName());
					h.getNode().put("is_deleted",equipBaseInfoBean.getIsDeleted()+"");
					h.getNode().put("use_status",equipBaseInfoBean.getUseStatus());
					h.getNode().put("category_id",equipBaseInfoBean.getCategoryId()+"");
					h.getNode().put("equip_model",equipBaseInfoBean.getEquipModel());
					h.getNode().put("supplier_id",equipBaseInfoBean.getSupplierId()+"");
					h.getNode().put("asset_number",equipBaseInfoBean.getAssetNumber());
					h.getNode().put("purchase_time",equipBaseInfoBean.getPurchaseTime()+"");
					h.getNode().put("purchase_type",equipBaseInfoBean.getPurchaseType());
					h.getNode().put("equip_category",equipBaseInfoBean.getEquipCategory());
					h.getNode().put("incharge_person",equipBaseInfoBean.getInchargePerson());
					h.getNode().put("production_date",equipBaseInfoBean.getProductionDate()+"");
					h.getNode().put("manufacture_date",equipBaseInfoBean.getManufactureDate()+"");
					h.getNode().put("emergency_contact",equipBaseInfoBean.getEmergencyContact());
					h.getNode().put("manufacture_number",equipBaseInfoBean.getManufactureNumber());
				}
			}
		}

		//对象转json
		JSONObject json = (JSONObject) JSONObject.toJSON(hotParentBean);
		String str = json.toString();
        bean.setHotDesc(str);
		equipHotPointRecordDao.editEquipHotPointRecord(bean);
		//查询热点图
		return equipHotPointRecordDao.queryEquipHotPointRecord(bean);
	}

	@Override
	public void deleteEquipHotPointRecord(EquipHotPointRecord bean) {
		//删除热点图
		equipHotPointRecordDao.deleteEquipHotPointRecord(bean);
	}
}
